<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="姓名">
              <el-input v-model="form.name" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="手机号">
              <el-input v-model="form.phone" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="支付时间" width="236">
              <el-date-picker
                v-model="form.notify_time"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态">
              <el-select v-model="form.status" placeholder="请选择" clearable>
                <el-option
                  v-for="(item,index) in statusType"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="2">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <!-- <el-button type="primary" @click="handleExport">导出</el-button> -->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div class="listDiv" v-loading="tableLoading">
      <el-table ref="tableData" :data="tableData" style="width: 100%">
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="50"
          align="center"
          :index="table_index"
        />
        <el-table-column label="订单编号" prop="order_sn" align="center" />
        <el-table-column label="姓名" prop="name" align="center" />
        <el-table-column label="手机号" prop="phone" align="center">
            <template slot-scope="scope">
            <span>{{scope.row.phone | phonereg}}</span>
          </template>
        </el-table-column>
        <el-table-column label="推荐人姓名" prop="recommend_name" align="center" />
        <el-table-column label="推荐人手机号" prop="recommend_phone" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.recommend_phone | phonereg}}</span>
          </template>
        </el-table-column>
        <el-table-column label="所在地区" prop="area" align="center" />
        <el-table-column label="详细地址" prop="address" align="center" />
        <el-table-column label="状态" prop="status_name" align="center"></el-table-column>
        <el-table-column label="物流单号" prop="logistics_number" align="center" />
        <el-table-column label="支付时间" prop="notify_time" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.notify_time | dateFn}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="text"
              v-if="scope.row.status_name == '待发货'"
              @click="delivery(scope.row.id)"
            >发货</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="form.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <!-- 发货弹窗 -->
    <el-dialog title="物流单号" :visible.sync="dialogFormVisible" width="368px" @close="close">
      <el-form ref="form1" :model="form1" label-width="130px" :inline="true" :rules="rules">
        <div class="allCenter">
          <el-form-item prop="logistics_number">
            <el-input v-model="form1.logistics_number" />
          </el-form-item>
        </div>
        <div class="right">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirm('form1')">确 定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getList, faHuo, orderExport } from "@/api/activityMange";
import commonFn from "@/utils/common";
export default {
  name: "activityList",
  data() {
    // 物流号匹配数字或字母
    const orderReg = (rule, value, callback) => {
      let reg = /^[0-9a-zA-Z]*$/g;
      if (!value) {
        return callback(new Error("请输入物流单号"));
      } else if (!reg.test(value)) {
        return callback(new Error("请输入正确的数字或字母"));
      } else {
        callback();
      }
    };
    return {
      form: {
        name: "",
        phone: "",
        status: "",
        notify_time: "",
        page: 1,
        pagesize: 10
      },
      form1: {
        logistics_number: "",
        id: ""
      },
      // 发货状态
      statusType: [
        { name: "全部", value: 0 },
        { name: "待发货", value: 1 },
        { name: "已发货", value: 2 }
      ],
      tableData: [],
      rules: {
        logistics_number: [
          {
            required: true,
            validator: orderReg,
            trigger: "blur"
          }
        ]
      },
      // 分页
      page_sizes: [10, 30, 50, 100], // 每页显示个数
      page_totals: 0, // 总条目数,
      tableLoading: true,
      dialogFormVisible: false
    };
  },
  filters: {
    // 时间戳转化
    dateFn(e) {
      if (e != 0) {
        return new Date(Number(e) * 1000).format("yyyy-MM-dd hh:mm:ss");
      } else {
        return "--";
      }
    }
  },
  created() {
    this.getList(this.form);
  },
  methods: {
    // 序号
    table_index(index) {
      return (this.form.page - 1) * this.form.pagesize + index + 1;
    },
    getList(e) {
      getList(e)
        .then(res => {
          if (res.data.code == 200) {
            this.tableData = res.data.data;
            this.page_totals = res.data.page_totals;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
          this.tableLoading = false;
        })
        .catch(err => {
          this.errorStatus(err.msg);
          this.tableLoading = false;
        });
    },
    // 发货弹窗
    delivery(id) {
      this.dialogFormVisible = true;
      this.form1.id = id;
    },
    // 确认发货
    confirm(e) {
      let datas = {
        id: this.form1.id,
        logistics_number: this.form1.logistics_number
      };
      this.$refs[e].validate(valid => {
        if (valid) {
          faHuo(datas)
            .then(res => {
              if (res.data.code == 200) {
                this.dialogFormVisible = false;
                this.getList(this.form);
              } else {
                this.$message({
                  showClose: true,
                  message: res.data.msg,
                  type: "warning"
                });
              }
            })
            .catch(err => {
              this.errorStatus(err.msg);
            });
        } else {
          this.$message({
            showClose: true,
            message: "请输入物流单号",
            type: "warning"
          });
          return;
        }
      });
    },
    // 查询
    handleSearch() {
      this.form.page = 1;
      this.getList(this.form);
    },
    // 导出
    handleExport() {
      let datas = { ...this.form };
      delete datas.page;
      delete datas.pagesize;
      orderExport(datas)
        .then(res => {
          commonFn.downFile("颜咖活动订单导出", res);
        })
        .catch(err => {
          this.errorStatus(err.msg);
        });
    },
    // 关闭弹窗清空信息
    close() {
      this.dialogFormVisible = false;
      this.form1.logistics_number = "";
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pagesize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },
    // // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    }
  }
};
</script>
<style scoped>
.el-range-editor--medium.el-input__inner {
  width: 236px;
}
.pagesCon {
  margin-top: 20px;
  text-align: right;
}
.allCenter {
  text-align: center;
}
.right {
  text-align: right;
}
</style>